package Java.view.game.douniu;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;

public class Player {
    private String name; //姓名
    private int allHappyDou; //原来欢乐豆
    private int betHappyDou; //赌注欢乐豆
    private int score; //得分
    private boolean isStop = false; //是否停止
    private boolean isWin = false; //是否胜利

    //服务端
    private Socket socket;
    public BufferedReader reader;
    public BufferedWriter writer;
    public int playerID;//玩家ID，用于和服务器对接,识别唯一身份
    public ArrayList<String> handCards=new ArrayList<>();//持有的手牌
    private Socket connection;//和服务器进行连接
    public ArrayList<String> getHandCards() {
        return handCards;
    }

    public void setHandCards(ArrayList<String> handCards) {
        this.handCards = handCards;
    }

    public Player(Socket socket) throws IOException {
        this.socket=socket;
        reader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        writer=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    }

    public Socket getSocket() {
        return socket;
    }

    //接收服务器所分发的手牌
    //startGame#索引#牌$
    public void receiveCardsFromServer(String card)
    {
        //按照空格分割
        String[]remaining=card.split(" ");
        //将remaining中的数据存入cards中
        for (String item : remaining) {
            handCards.add(item);
        }
    }
    // 发送数据到服务器
    public void sendDataToServer(String data) {
        // 利用连接发送数据给服务器
        try {
            OutputStream outputStream = connection.getOutputStream();
            PrintWriter writer = new PrintWriter(outputStream, true);
            writer.println(data);
        } catch (IOException e) {
            System.out.println("发送数据失败: " + e.getMessage());
        }
    }

    // 接收服务器数据
    public String receiveDataFromServer() {
        String receivedData = null;
        BufferedReader reader = null;
        while(receivedData==null){
            try {
                connection.setSoTimeout(500000);
                InputStream inputStream = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(inputStream));
                receivedData = reader.readLine();
                System.out.println(receivedData);
            } catch (IOException e) {
                System.out.println("接收数据失败: " + e.getMessage());
            }
        }

        return receivedData;
    }


    public int getPlayerID() {
        return playerID;
    }

    public void setPlayerID(int playerID) {
        this.playerID = playerID;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public Player(String name) {
        this.name = name;
        this.connection= douniuFX_lian.socket;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAllHappyDou() {
        return allHappyDou;
    }

    public void setAllHappyDou(int allHappyDou) {
        this.allHappyDou = allHappyDou;
    }

    public int getBetHappyDou() {
        return betHappyDou;
    }

    public void setBetHappyDou(int betHappyDou) {
        this.betHappyDou += betHappyDou;
    }

    public boolean isStop() {
        return isStop;
    }

    public void setStop(boolean stop) {
        isStop = stop;
    }

    public boolean isWin() {
        return isWin;
    }

    public void setWin(boolean win) {
        isWin = win;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}